<?php
require_once("../../Config.php");
require_once("../../Class/ClassFunc.php");
require_once("../../Class/ClassMysql.php");
require_once("../../Class/ClassCheck.php");
require_once("../../Class/ClassResult.php");

//json头
header("Content-type: application/json");

session_start();
if(!isset($_SESSION["name"])) {
    exit(Result::jsonReturn(1, "未登录"));
}

$uid = (int)$_SESSION["id"];
$uname = $_SESSION["name"];
$uemail = $_SESSION["email"];
$upower = $_SESSION["power"];


$db = new DB();
if(Check::isEmpty($_GET["belong"])) {
    // 校验是否为空
    if(!Check::isEmpty($_GET["id"])) {
        if(Check::isNum($_GET["id"]) && (int)$_GET["id"]>0) {
            $id = (int)$_GET["id"];
            $sql = "SELECT * FROM qilian_contents WHERE id={$id}";
            $res = array();
            if($res = $db->getOne($sql)) {
                $res = getBelong($res, $id, $db);
                exit(Result::jsonReturn(0, "查询成功", $res));
            }else {
                exit(Result::jsonReturn(1, "查询失败"));
            }
        }else {
            exit(Result::jsonReturn(1, "参数格式有误"));
        }
    }else {
        $sql = "SELECT * FROM qilian_contents ORDER BY id DESC";
        $res = array();
        if($res = $db->getAll($sql)) {
            foreach($res as $index=>$value) {
                $res[$index] = getBelong($value, $value["id"], $db);
            }
            exit(Result::jsonReturn(0, "查询成功", $res));
        }else {
            exit(Result::jsonReturn(1, "查询失败"));
        }    
    }    
}else if($_GET["belong"] == "self") {
    // 校验是否为空
    if(!Check::isEmpty($_GET["id"])) {
        if(Check::isNum($_GET["id"]) && (int)$_GET["id"]>0) {
            $id = (int)$_GET["id"];
            $sql = "SELECT * FROM qilian_contents WHERE id={$id} AND user_id={$uid}";
            $res = array();
            if($res = $db->getOne($sql)) {
                $res = getBelong($res, $id, $db);
                exit(Result::jsonReturn(0, "查询成功", $res));
            }else {
                exit(Result::jsonReturn(1, "查询失败"));
            }
        }else {
            exit(Result::jsonReturn(1, "参数格式有误"));
        }
    }else {

        $sql = "SELECT * FROM qilian_contents WHERE user_id={$uid} ORDER BY id DESC";
        $res = array();
        if($res = $db->getAll($sql)) {
            foreach($res as $index=>$value) {
                $res[$index] = getBelong($value, $value["id"], $db);
            }
            exit(Result::jsonReturn(0, "查询成功", $res));
        }else {
            exit(Result::jsonReturn(1, "查询失败"));
        }    
    }    
}else if($_GET["belong"] == "other") {
    // 校验是否为空
    if(!Check::isEmpty($_GET["id"])) {
        if(Check::isNum($_GET["id"]) && (int)$_GET["id"]>0) {
            $id = (int)$_GET["id"];
            $sql = "SELECT * FROM qilian_contents WHERE id={$id} AND user_id!={$uid}";
            $res = array();
            if($res = $db->getOne($sql)) {
                $res = getBelong($res, $id, $db);
                exit(Result::jsonReturn(0, "查询成功", $res));
            }else {
                exit(Result::jsonReturn(1, "查询失败"));
            }
        }else {
            exit(Result::jsonReturn(1, "参数格式有误"));
        }
    }else {
        $sql = "SELECT * FROM qilian_contents WHERE user_id!={$uid} ORDER BY id DESC";
        $res = array();
        if($res = $db->getAll($sql)) {
            foreach($res as $index=>$value) {
                $res[$index] = getBelong($value, $value["id"], $db);
            }
            exit(Result::jsonReturn(0, "查询成功", $res));
        }else {
            exit(Result::jsonReturn(1, "查询失败"));
        }    
    }    
}else if($_GET["belong"] == "order") {
    // 校验是否为空
    if(!Check::isEmpty($_GET["id"])) {
        if(Check::isNum($_GET["id"]) && (int)$_GET["id"]>0) {
            $id = (int)$_GET["id"];
            $sql = "SELECT * FROM qilian_contents WHERE id={$id}";
            $res = array();
            if($res = $db->getOne($sql)) {
                $res = getBelong($res, $id, $db);
                exit(Result::jsonReturn(0, "查询成功", $res));
            }else {
                exit(Result::jsonReturn(1, "查询失败"));
            }
        }else {
            exit(Result::jsonReturn(1, "参数格式有误"));
        }
    }else {
        $sql = "SELECT * FROM qilian_contents WHERE user_id={$uid} ORDER BY id DESC";
        $res = $db->getAll($sql);
        $sql_other = "SELECT * FROM qilian_contents WHERE user_id!={$uid} ORDER BY id DESC";
        $res_other = $db->getAll($sql_other);
        $all = array_merge($res, $res_other);
        foreach($all as $index=>$value) {
            $all[$index] = getBelong($value, $value["id"], $db);
        }
        exit(Result::jsonReturn(0, "查询成功", $all));
    }
}else {
    exit(Result::jsonReturn(1, "参数错误"));
}

function getBelong($arr, $id, $db) {
    $sql = "SELECT list_id FROM qilian_relationships WHERE content_id={$id}";
    $idArr = $db->getAll($sql);
    foreach($idArr as $index=>$value) {
        $idArr[$index] = $value["list_id"];
    }
    $nameArr = array();
    foreach($idArr as $index=>$value) {
        $sql_l = "SELECT name FROM qilian_lists WHERE id={$value}";
        array_push($nameArr, $db->getOne($sql_l)["name"]);
    }
    $arr["belong"] = $idArr;
    $arr["belongname"] = $nameArr;

    $sql_at = "SELECT * FROM qilian_attachments WHERE content_id={$id}";
    $attachments = $db->getAll($sql_at);
    foreach($attachments as $key=>$val) {
        $attachments[$key]['title'] = CDN.$val['title'];
    }
    $arr["attachments"] = $attachments;
    return $arr;
}

?>